package com.sgb.kjwl.viewmodel.category;

import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.repository.CategoryRepertory;
import com.swgk.core.mmkvutil.CacheMarker;
import com.sgb.kjwl.view.ui.fragment.CategoryLeaseFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelperNew;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.util.MToast;

import java.lang.reflect.Type;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class CategoryLeaseFragmentModel extends BaseViewModel {
    private CategoryRepertory repertory;
    private CategoryLeaseFragment fragment;

    public CategoryLeaseFragmentModel(CategoryLeaseFragment fragment, CategoryRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    /**
     * 获取租赁全部分类
     */
    public void getLeaseCategoryModel() {
        DialogHelperNew.showProgressDialog(fragment.getActivity(), "数据加载中...").setCanceledOnTouchOutside(false);
        Type type = new TypeToken<List<MenuEntity<MenuEntity<MenuEntity>>>>() {
        }.getType();
        Object object = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.CacheGoryLeaseModel, type);
        if (null != object) {
            List<MenuEntity<MenuEntity<MenuEntity>>> cacheList = (List<MenuEntity<MenuEntity<MenuEntity>>>) object;
            if (cacheList.size() > 0) {
                fragment.showCategoryView(cacheList);
                DialogHelperNew.dismissProgressDialog();
            }
        }

        APIManager.getInstance().getLeaseAPI().getLeaseAllCategoryList().enqueue(new Callback<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>> call, Response<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>> response) {
                DialogHelperNew.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    //获取缓存哈希值
                    String mHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.CacheGoryLeaseModel);
                    //获取从网络请求下来的数据的哈希值
                    String mHashNet = DatalUtils.getInstance().strToHashKey(response.body().getData());
                    if (!mHash.equals(mHashNet)) {
                        fragment.showCategoryView(response.body().getData());
                        DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.CacheGoryLeaseModel, response.body().getData());
                    }

                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>> call, Throwable t) {
                DialogHelperNew.dismissProgressDialog();
            }
        });
    }

}
